window.addEventListener('scroll', function() {
    const navbar = document.getElementById('navbar');
    if (window.scrollY > 50) {
        navbar.classList.add('scrolled');
    } else {
        navbar.classList.remove('scrolled');
    }
});


// 轮播
// 获取轮播图
async function getBanners(){
  // 用axios获取轮播图
  const imgElements = document.querySelectorAll('.mySlides img');
  const res=await axios.get('http://47.120.4.197:8082/user/images');
  // console.log(res.data.data.rows);
  res.data.data.rows.forEach((row, index) => {
    // console.log(row.url);
    imgElements[index].src = row.url;
  });
}
window.addEventListener('load', getBanners);

var slideIndex = 0;
var intervalId; // 用于存储 setInterval 返回的 ID
let slides = document.getElementsByClassName("mySlides");
showSlides(slideIndex);
function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
  clearInterval(intervalId);
  intervalId = setInterval(function() {
    plusSlides(1);
  }, 2500);
}

// 鼠标悬停时停止自动播放
document.querySelector('.slideshow-container').addEventListener('mouseenter', function() {
  clearInterval(intervalId);
});

// 鼠标移开时恢复自动播放
document.querySelector('.slideshow-container').addEventListener('mouseleave', function() {
  intervalId = setInterval(function() {
    plusSlides(1);
  }, 3000);
});

// 初始设置自动播放
 intervalId = setInterval(function() {
  plusSlides(1);
}, 3000);

function showSlides(n) {
  var i;
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
  }
  slides[slideIndex-1].style.display = "block";
}
function totop() {
  $('html, body').animate({ scrollTop: 0 }, 'smooth');
}

// 将函数作为回调传递
$(document).ready(totop);

// 获得创明发展历程
async function getJourneyInfo(){
  const res=await axios.get('http://47.120.4.197:8082/user/journeyInfos');
  // console.log(res);
  const data = res.data.data;
  console.log(data);
  const datesContainer = document.getElementById('dates');
  // console.log(datesContainer);
    // 使用 map 函数遍历数据并生成日期列表
    data.map(item => {
        const li = document.createElement('li');
        const datePart = item.time.split(' ')[0]; // 获取 "YYYY-MM-DD"
        li.innerHTML = `<a href="#${datePart}">${datePart}</a>`; // 数据中有 time 字段
        datesContainer.appendChild(li);
    });
    const issuesContainer = document.getElementById('issues');

    // 使用 map 函数遍历数据并生成内容列表
    data.map(item => {
        const li = document.createElement('li');
        const datePart = item.time.split(' ')[0]; // 获取 "YYYY-MM-DD"
        li.id = datePart; // 假设每个项都有 time 字段作为 ID
        // <img src="${item.imageUrl}" /> 
        li.innerHTML = `
            <img src="./image/banner-1.jpg"/> 
            <h1>${item.title}</h1>
            <p>${item.content}</p>
        `;
        issuesContainer.appendChild(li);
    });

    // 渲染完之后再载入timeline
    $(function(){
			$().timelinr({
				autoPlay: 'true',
				autoPlayDirection: 'forward',
				autoPlayPause:4000
			})
		});
}
window.addEventListener('load', getJourneyInfo);
window.addEventListener('load', checkLogin);
function checkLogin() {
  const token = localStorage.getItem('token');
  const isLoging=document.querySelector('.auth')

  if (token) {
    isLoging.style.display = 'none';
  } else {
    isLoging.style.display = 'block';
  }
}
// 监测 localStorage 中 token 的变化
window.addEventListener('storage', (event) => {
  if (event.key === 'token' && event.newValue === null) {
      // 自动刷新页面
      window.location.reload();
  }
});



const mainActionButton = document.querySelector('.main-action');

mainActionButton.addEventListener('click', function(event) {
  const token = localStorage.getItem('token');
  const defaultLink = "https://qm.qq.com/cgi-bin/qm/qr?k=IMisyCWeZigH7VwjIFr3Ci1OKnJnucQt&jump_from=webapi&authKey=M+8AC53BCB1+GSrZCzEyJGfOrMDATf3JCloMKAw3Inn1rbmHcnBdzHt6h5aptwZ3"; // 默认跳转链接
  const loginLink = '../../login_register/login.html'; // 登录链接

  if (token) {
    window.location.href = defaultLink;
  } else {
    window.location.href = loginLink;
  }
});

